package cn.miw.android.bdmp3;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import cn.miw.android.base.MiwUtil;
import cn.miw.android.bdmp3.model.Catlogs;
import cn.miw.android.bdmp3.model.Catlogs.Catlog;
import cn.miw.android.bdmp3.service.Util;
import cn.miw.android.listViewAdapter.IListViewInitor;
import cn.miw.android.listViewAdapter.ListViewAdapter;

public class OnlineActivity extends Base implements OnItemClickListener {
	
	class ViewHolder {
		ImageView RankPic;
		TextView RankName;
	}
	private List<Catlog> rankLists = new ArrayList<Catlogs.Catlog>();
	private ListViewAdapter adapter;
	private IListViewInitor init=new IListViewInitor() {
		
		public View initView() {
			ViewHolder holder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View convertView = inflater.inflate(R.layout.rank_item, null);
			holder.RankName = (TextView) convertView.findViewById(R.id.RankName);
			holder.RankPic = (ImageView) convertView.findViewById(R.id.RankPic);
			convertView.setTag(holder);
			return convertView;
		}
		
		public View fillView(View view, Object data,String MOTHED,int position) {
			ViewHolder holder =  (ViewHolder) view.getTag();
			holder.RankPic.setImageDrawable(OnlineActivity.this.getResources().getDrawable(R.drawable.ic_launcher));
			holder.RankName.setText(((Catlog)data).getName());
			return null;
		}
	};

	@Override
	public void BuildData(Void... p) {
		String url = "http://box.zhangmen.baidu.com/x?op=3&list_cat=1";
		xml = Util.getWebContent(url, "gbk");
		Catlogs catlogs = (Catlogs) MiwUtil.parseXML(xml, Catlogs.class);
		rankLists.addAll(catlogs.getCatlogs());
	}

	@Override
	public void ShowData() {
		adapter.notifyDataSetChanged();
	}

	private void Init() {
		Dialog.setMessage("下载榜单信息中...");
		((TextView) findViewById(R.id.title)).setText("音乐榜");
		adapter = new ListViewAdapter(init, rankLists,"");
		GridView gridView = (GridView) findViewById(R.id.gridRank);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(this);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ranks);
		Init();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (rankLists.size() == 0)
			miwHandler();
	}

	public void onItemClick(AdapterView<?> owner, View view, int position, long id) {
		Catlog catlog = rankLists.get(position);
		Intent intent = new Intent();
		intent.setClass(this, TopActivity.class);
		intent.putExtra("catlog", catlog);
		startActivity(intent);
	}

}
